﻿
Imports System.Net
Imports System.DirectoryServices
Imports System.Xml
Imports System.IO
Imports ActiveDs



Public Class Form1
    Dim maxUser As Integer = 5
    Dim maxDC As Integer = 3
    Dim domain As String
    Dim domainLDAP As String
    Dim LockedUsers(maxUser) As String
    Dim domainControllers(maxDC) As String



    Private Sub btnUnlock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnlock.Click
        Dim userindex As Integer
        If lstUsers.SelectedIndex = -1 Then
            MessageBox.Show("Please select a user to unlock")
        Else
            userindex = lstUsers.SelectedIndex
            Debug.Print(LockedUsers(userindex))

            Unlock(LockedUsers(userindex))
        End If



    End Sub

    Public Function getLockedUsers()
        Dim strError As String
        lstUsers.Items.Clear()
        'lblStatus.Text = Nothing


        Try
            Dim child As New System.DirectoryServices.DirectoryEntry("LDAP://" & domainLDAP)
            Dim searcher As New DirectorySearcher(child)
            Dim results As SearchResultCollection

            searcher.Filter = "(&(objectClass=User)(lockoutTime>=1))"
            searcher.CacheResults = False
            results = searcher.FindAll


            Dim i As Integer = 0
            For Each result In results

                ' lstUsers.Items.Add(result.Properties("name")(0))

                If i < maxUser Then
                    LockedUsers(i) = result.Properties("sAMAccountName")(0)
                    Dim RawLastLogon As Int64 = CLng(result.Properties("lockouttime")(0))
                    Dim LastLogonDate As DateTime = DateTime.FromFileTime(RawLastLogon)
                    lstUsers.Items.Add(LockedUsers(i) & "   -   " & LastLogonDate)
                    Debug.Print("Number " & i & " Users")
                End If


                If i + 1 > maxUser Then
                    Debug.Print("Number " & i & " User was deleted")
                    lblStatus.Text = "To many locked users's. Purchase the PRO version"
                    LockedUsers(i) = Nothing
                End If

                i = 1 + i
            Next




        Catch ex As Exception
            strError = ex.ToString
        End Try


        Return Nothing






    End Function

    Public Sub getDomain()
        Dim sr As New StreamReader("domain.ini")
        Dim i As Integer = 0
        Debug.Print("Looking for domain.ini")
        Do While sr.Peek <> -1

            If i = 0 Then
                domain = sr.ReadLine
            End If

            If i = 1 Then
                domainLDAP = sr.ReadLine
            End If

            i = i + 1
        Loop
        sr.Close()

    End Sub

    Public Sub getDC()
        'lblStatus.Text = "Work damit"
        Dim sr As New StreamReader("dc.ini")
        Dim i As Integer = 0
        Do While sr.Peek <> -1
            If i < maxDC Then
                domainControllers(i) = sr.ReadLine
                Debug.Print("Number " & i & " DC")
            End If


            If i + 1 > maxDC Then
                Debug.Print("Number " & i & " DC was deleted")
                lblStatus.Text = "To many DC's. You must purchase the PRO version"
                domainControllers(i) = Nothing
            End If

            i = i + 1
        Loop
        sr.Close()
    End Sub

    Public Function ListAllUSers()
        'either hard code or obtain from variable the computer name/user
        Dim DomainString As String = domain
        Dim UserString As String = lstUsers.SelectedItem

        'just set the user/group object to object 
        Dim UserObj, GroupObj As Object
        Dim List As String = Nothing

        'specify what object to obtain
        UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

        'enumerate the objects

        For Each GroupObj In UserObj.Groups
            List = List & GroupObj.Name & vbCrLf
        Next

        ' Form3.rtbSystemRpt.Text = List
        'rtbWMIQueriesCont.Text = List

        UserObj = Nothing
        GroupObj = Nothing
        Return List
    End Function

    Private Sub Unlock(ByVal username As String)
        'lblStatus.Text = UnlockAccount(username, "BSWB-DC01")
        Dim i As Integer = 0
        For Each item In domainControllers
            If Not domainControllers(i) = Nothing Then
                Debug.Print(domainControllers(i))

                lblStatus.Text = UnlockAccount(username, domainControllers(i))
            End If
            i = 1 + i
        Next

    End Sub



    Public Function UnlockAccount(ByVal username As String, ByVal DC As String)
        Dim strError As String = Nothing
        Try
            If Not DC = Nothing Then


                Dim child As New System.DirectoryServices.DirectoryEntry("LDAP://" & DC & ":389/" & domainLDAP)
                Dim searcher As New DirectorySearcher(child)
                Dim result As SearchResult
                Dim userEntry As DirectoryEntry
                searcher.Filter = "(SAMAccountName=" & username & ")"
                searcher.CacheResults = False
                result = searcher.FindOne
                userEntry = result.GetDirectoryEntry

                With userEntry
                    userEntry.Properties("LockOutTime").Value = 0
                End With
                userEntry.CommitChanges()
                strError = "Account was unlocked"
            End If

 Catch ex As Exception
            Debug.Print(ex.ToString)
            strError = "Error with one of the DC's"
        End Try

        Return strError
    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set Application Description
        'Redim after we figure out which license we have.

        Debug.Print("Running")

        getDomain()
        getLockedUsers()
        getDC()
        lblDomain.Text = "Domain:  " & domain

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        getLockedUsers()
        ' lblStatus.Text = "Status: Refreshed List"
    End Sub

    Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
        About.Show()

    End Sub
End Class
